package com.pybox.service;

import com.pybox.dto.TWeekTimeDto;
import com.pybox.entity.TWeekTime;
import com.pybox.repository.TWeekTimeRepository;
import com.pybox.spec.TWeekTimeSpec;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 描述：TWeekTime Service
 *
 * @author wuyingkai
 * @date 2018-09-15
 */

@Slf4j
@Service
@Transactional
public class TWeekTimeService {

    @Autowired
    private TWeekTimeRepository repository;

    /**
     * 创建TWeekTime
     *
     * @param source
     * @return
     */
    public boolean create(TWeekTimeDto source) {

        TWeekTime target = new TWeekTime();
        BeanUtils.copyProperties(source, target);
        for (Integer h : source.getHours()) {
            try {
                Method m = target.getClass().getDeclaredMethod("setHour" + h, Integer.class);
                m.invoke(target, h);
            } catch (Exception e) {
                log.error("method not exist");
            }
        }
        target.setCtime(new Timestamp(System.currentTimeMillis()));
        repository.save(target);
        return true;

    }

    /**
     * 根据ID删除TWeekTime
     *
     * @param id
     * @return
     */
    public boolean delete(Integer id) {

        repository.delete(id);

        return true;

    }

    /**
     * 更新TWeekTime
     *
     * @param id
     * @param source
     * @return
     */
    public boolean update(Integer id, TWeekTimeDto source) {

        TWeekTime exist = repository.findOne(id);
        if (null == exist) {
            throw new RuntimeException("记录不存在");
        }
        exist.setUtime(new Timestamp(System.currentTimeMillis()));
        BeanUtils.copyProperties(source, exist, new String[]{"id"});
        repository.save(exist);

        return true;

    }

    /**
     * 根据ID查询TWeekTime
     *
     * @param id
     * @return
     */
    public TWeekTimeDto detail(Integer id) {

        TWeekTime exist = repository.findOne(id);
        if (null == exist) {
            throw new RuntimeException("记录不存在");
        }

        TWeekTimeDto target = new TWeekTimeDto();
        BeanUtils.copyProperties(exist, target);

        return target;

    }

    /**
     * 分页查询查询TWeekTime
     *
     * @param pageNo
     * @param pageSize
     * @param paramMap
     * @return
     */
    public Page<TWeekTimeDto> page(Integer pageNo, Integer pageSize, Map<String, String> paramMap) {

        Pageable pageable = new PageRequest(pageNo - 1, pageSize);
        Page<TWeekTime> data = repository.findAll(new TWeekTimeSpec(paramMap), pageable);

        List<TWeekTimeDto> list = new ArrayList<>();
        for (TWeekTime obj : data.getContent()) {
            TWeekTimeDto dto = new TWeekTimeDto();
            BeanUtils.copyProperties(obj, dto);
            list.add(dto);
        }

        Page<TWeekTimeDto> page = new PageImpl<>(list, pageable, data.getTotalElements());

        return page;

    }
}